package XOrules;

import com.sun.xml.internal.ws.api.pipe.Fiber;

import java.util.*;
import java.lang.*;
import java.math.*;
import common.*;

public class server extends KA {
    
    public final static String USR_SERVER = "Server";
    public final static String KA_GAME = "KA_GAME";
    public final static String KA_WORK = "KA_WORK";

    public Vector<TreeMap<String, String>> stream;
    public Vector<String> users;
    public String state;
    public GameManager game;
    
	public server(){
        this.name = USR_SERVER;
		super(null);
		stream = new Vector<TreeMap<String, String>>();
		users = new Vector<String>();
    }

	public void start(){
		//System.out.println(time.ctime());
		System.out.println("start");

		while (true){
			while (stream.isEmpty())
				Helper.sleep(100);

			TreeMap<String, String> msg = this.stream.get(0); //pop
			Request req = new Request(msg);
			req = super.doRequest(req);
			this.returnResult(req);

			if (this.state == KA_0)
				break;
        }
		this.stop();
    }

	public void stop(){
		//liba.stop();
		System.out.println("stop");
		//System.out.println(time.ctime());
		//sys.exit();
    }

    private Request _doRequest(Request req){
	return Evaluator.callMethod(this, req.type, req);
    }

	public void returnResult(Request req){
		if (!req.isAns)
			req.who = this.name;
		String fromWho = req.who;
		String return_type = "return_" + req.type;
		//liba.sendTo(req.usr, fromWho + ";" + return_type + ";" + req.getAnswer());
    }

	//############################################
	//#реализация конкретных действий КА

    public Request startServer(Request req) {
		if (req.usr == this.name){
			//liba.init();
			//liba.setValue("timeout", 60 * 60);
			//liba.subscribe({}, this.stream);
			//liba.subscribe({}, 'stdout');
			//liba.startServer();

			super.setState(this, KA_WORK);
			return req.Answer(this, true);
        }
		return req;
    }

	public Request stopServer(Request req) {
		super.setState(this, KA_0);
		return req.Answer(this, true);
    }

	public Request hi(Request req) {
		if (this.users.count(req.usr) == 0)
			this.users.append(req.usr);
		return req.Answer(this, true);
    }

	public Request bye(Request req) {
		return req.Answer(this, true);
    }

	public Request exit(Request req) {
		if (this.users.count(req.usr) > 0)
			this.users.remove(req.usr);
		return req.Answer(this, true);
    }

	public Request newGame(Request req) {
		gameName = req.body;
		if (games.keys().count(gameName) == 0)
			return req.Answer(this, new Vector<Object>(false, "no game this name"));
		super.setState(this, KA_GAME);
		this.subKA = Evaluator.callMethod(this, games[gameName], liba.mail);
		for (String usr: this.users)
			; //liba.mail({MSG_USR:this.name, MSG_TYPE: "sendMsg", MSG_BODY:  '["' + usr + '", "newGame"]'});
		return req.Answer(this, true);
    }

	public Request stopGame(Request req) {
		this.game = null;
		super.setState(this, KA_WORK);
		for (String usr: this.users)
			; //liba.mail({MSG_USR:this.name, MSG_TYPE: "sendMsg", MSG_BODY: '["' + usr + '", "stopGame"]'});
		return req.Answer(this, true);
    }

	public Request sendMsg(Request req) {
		//liba.sendTo(req.body[0], req.usr + ";" + "msg" + ";" + json.dumps(req.body[1]));
		return req.Answer(this, true);
    }

	public Request getState(Request req) {
		return req.Answer(this, this.state);
    }

	public Request saveLog(Request req) {
		/*f = open(req.body + ".log", "w");
		f.write(str(this.log));*/
		return req.Answer(this, true);
    }

	public Request getLog(Request req) {
		/*if (req.body== ""):
			return req.Answer(this, this.log);
		else:
			f = open(req.body+ ".log", "r");
			return req.Answer(this, f.read()); */
		return req.Answer(this, true);
    }

	public Request saveGame(Request req) {
		/*f = open(req.body+ ".gm", "w");
		f.write(this.subKA.getSituation(this.name));
		f.close();*/
		return req.Answer(this, true);
    }

	public Request loadGame(Request req) {
		/*gameName = req.body[0];
		if (games.keys().count(gameName) == 0):
			return req.Answer(this, [false, "no game this name"]);
		f = open(req.body[1] + ".gm", "r");
		situation = f.read();
		f.close();

		super.setState(this, KA_GAME);
		this.subKA = eval(games[gameName])(liba.mail) {
		this.subKA.setSituation(situation);
		for usr in this.users:
			liba.mail({MSG_USR:this.name, MSG_TYPE: "sendMsg", MSG_BODY:  '["' + usr + '", "newGame"]'});  */
		return req.Answer(this, true);
    }

	public Request timeout(Request req) {
		this.stopServer(req);
		return req.Answer(this, true);
    }

	public Request getSituation(Request req) {
		if (this.state == KA_GAME)
			return req.Answer(this, this.subKA.getSituation(req.usr));
		else
			return req.Answer(this, false);
    }
}

